package com.dtstack.rocketmq.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * Like
 *
 * <p>
 * </p>
 *
 * @author chuanbai
 * @date 2024/9/27
 */
public class Like {


//    zhangsan,13211234 Elm St, Anytown, USA, USA
//    zhangsan,1234 Elm St, Anytown, USA, USA
    public static void main(String[] args) {
        Boolean b = checkSimilarity("zhangsan,13211234 Elm St, Anytown, USA, USA", "zhangsan,1234 Elm St, Anytown, USA");
        System.out.println(b);
    }

    /**
     * 两个字符串的相似度
     * @param s1
     * @param s2
     *
     * 动态规划算法来计算两个字符串的最长公共子序列（Longest Common Subsequence, LCS），然后基于这个最长公共子序列的长度来计算相似度
     * 最长公共子序列的长度存储在 lcs 变量中；相似度是通过将 lcs 除以两个字符串中较长的长度来计算的，这样可以避免除以零的情况
     *
     * @return Boolean
     */
    public static Boolean checkSimilarity(String s1, String s2){
        if (StringUtils.isBlank(s1)){
            s1 = "";
        }
        if (StringUtils.isBlank(s2)){
            s2 = "";
        }

        int len1 = s1.length();
        int len2 = s2.length();
        int[][] dp = new int[len1 + 1][len2 + 1];
        for (int i = 0; i <= len1; i++) {
            for (int j = 0; j <= len2; j++) {
                if (i == 0 || j == 0) {
                    dp[i][j] = 0;
                } else if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        int lcs = dp[len1][len2];
        double similarity = (double) lcs / Math.max(len1, len2);

        // 判断相似度大于等于95%
        if (similarity >= 0.95){
            return Boolean.TRUE;
        }else {
            return Boolean.FALSE;
        }

    }
}
